<?php
namespace app\admin\controller;

use app\AdminCollection;
use app\model\Noticemodel;
use My\Tool;
use My\Validate;

class Notice extends AdminCollection
{

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $input = input('');
        $page = isset($input['page']) ? $input['page'] : 1;
        $where = array();
        $list = Noticemodel::pageService($where);
        $orderkey = pagesize * ($page - 1);
        $_list = array();
        foreach ($list as $key => $val) {
            $_t = $val;
            $_t['orderkey'] = $orderkey += 1;
            $_t['inputtime'] = $val['inputtime'] != 0 ? date('Y-m-d H:i:s', $val['inputtime']) : '0000-00-00 00:00:00';
            $_t['deleteurl'] = url('notice/delete', array('id' => $val['id']));
            $_t['updateurl'] = url('notice/update', array('id' => $val['id']));
            $_list[] = $_t;
        }
        $pages = $list->render();
        $this->assign('list', $_list);
        $this->assign('pages', $pages);
        return $this->fetch();
    }

    /**
     * 编辑公告
     * @return mixed
     */
    public function update()
    {
        $submit = input('submit');
        if ($submit) {
            try {
                $input = input('');
                $where = $input['where'];
                $data = $input['info'];
                $title = $data['title'];
                $content = $data['content'];
                if (Validate::checkNull($title)) throw new \Exception("请输入标题");
                if (Validate::checkNull($content)) throw new \Exception("请输入内容");
                $data['updatetime'] = time();
                $return = Noticemodel::updateServer($where, $data);
                if ($return) {
                    $this->returnSuccess("提交数据成功");
                } else {
                    throw new \Exception("没有数据更新...");
                }
            } catch (\Exception $e) {
                $this->returnError($e->getMessage());
            }
        } else {
            try {
                $id = input('id');
                if (Validate::checkNull($id)) throw new \Exception("操作失败");
                $findService = Noticemodel::findService(['id' => $id]);
                if (Validate::checkEmpty($findService)) throw new \Exception("操作失败");
                $this->assign('findService', $findService);
                return $this->fetch();
            } catch (\Exception $e) {
                $this->returnError($e->getMessage());
            }
        }
    }

    /**
     * 删除公告
     */
    public function delete()
    {
        try {
            $id = input('id');
            if (Validate::checkNull($id)) throw new \Exception("删除失败");
            $photosfind = Noticemodel::findService(['id' => $id]);
            if (Validate::checkEmpty($photosfind)) throw new \Exception("删除失败");
            $return = Noticemodel::deleteService(['id' => $id]);
            if ($return) {
                $this->returnSuccess("删除成功");
            } else {
                throw new \Exception("删除失败");
            }
        } catch (\Exception $e) {
            $this->returnError($e->getMessage());
        }
    }

    /**
     * 添加公告
     * @return mixed
     */
    public function add()
    {
        $submit = input('submit');
        if ($submit) {
            try {
                $input = input('');
                $data = $input['info'];
                $title = $data['title'];
                $content = $data['content'];
                if (Validate::checkNull($title)) throw new \Exception("请输入标题");
                if (Validate::checkNull($content)) throw new \Exception("请输入内容");
                $data['title'] = $title;
                $data['content'] = $content;
                $data['admin_id'] = $this->getAdmin();
                $data['inputtime'] = time();
                $data['updatetime'] = time();
                $return = Noticemodel::addService($data);
                if ($return) {
                    $this->returnSuccess("提交数据成功");
                } else {
                    throw new \Exception("提交数据失败");
                }
            } catch (\Exception $e) {
                $this->returnError($e->getMessage());
            }
        } else {
            return $this->fetch();
        }
    }
}
